<?php

namespace tmcore\base;

use think\App;
use think\Request;
use tmcore\services\CacheService;
use tmcore\services\addons\UtilService;

/**
 * 插件基类
 */
abstract class BaseAddons
{
    // app容器
    protected App $app;
    // 请求对象
    protected Request $request;
    // 插件标识
    protected string $name;
    // 插件路径
    protected string $addon_path;
    // 插件信息
    protected string $addon_info;
    // 插件配置
    protected string $addon_config;
    // 插件事件
    protected string $addon_event;
    // 插件命令
    protected string $addon_command;

    /**
     * 构造函数
     */
    public function __construct()
    {
        $this->app = app();
        $this->request = $this->app->request;
        $this->name = $this->getName();
        $this->addon_path = UtilService::getAddonsPath() . $this->name . DIRECTORY_SEPARATOR;
        $this->addon_info = "addon_{$this->name}_info";
        $this->addon_config = "addon_{$this->name}_config";
        $this->addon_event = "addon_{$this->name}_event";
        $this->addon_command = "addon_{$this->name}_command";

        // 控制器初始化
        $this->initialize();
    }

    /**
     * 初始化方法
     */
    protected function initialize()
    {}

    /**
     * 获取插件标识
     * @return string
     */
    protected function getName(): string
    {
        $class = get_class($this);
        list(, $name, ) = explode('\\', $class);
        $this->request->addon = $name;
        return $name;
    }

    /**
     * 获取插件信息
     * @param bool $is_refresh 是否刷新
     * @return array
     */
    final public function getInfo($is_refresh = false): array
    {
        $info = CacheService::get($this->addon_info, []);
        if($is_refresh) {
            $info = [];
        } else {
            if ($info) return $info;
        }

        $infoFile = $this->addon_path . 'info.php';
        if (is_file($infoFile)) {
            $tempArr = (array) include $infoFile;
            foreach ($tempArr as $key => $value) {
                $info[$key] = $value;
            }
            unset($tempArr);
        }

        CacheService::set($this->addon_info, $info);
        return $info ?? [];
    }

    /**
     * 获取配置信息
     * @param bool $is_refresh 是否刷新
     * @return array
     */
    final public function getConfig($is_refresh = false): array
    {
        $config = CacheService::get($this->addon_config, []);
        if($is_refresh) {
            $config = [];
        } else {
            if ($config) return $config;
        }

        $configFile = $this->addon_path . 'config.php';
        if (is_file($configFile)) {
            $tempArr = (array) include $configFile;
            foreach ($tempArr as $key => $value) {
                $config[$key] = $value;
            }
            unset($tempArr);
            CacheService::set($this->addon_config, $config);
        }

        return $config;
    }

    /**
     * 获取事件信息
     * @param bool $is_refresh 是否刷新
     * @return array
     */
    final public function getEvent($is_refresh = false): array
    {
        $event = CacheService::get($this->addon_event, []);
        if($is_refresh) {
            $event = [];
        } else {
            if ($event) return $event;
        }

        $eventFile = $this->addon_path . 'event.php';
        if (is_file($eventFile)) {
            $tempArr = (array) include $eventFile;
            foreach ($tempArr as $key => $value) {
                $event[$key] = $value;
            }
            unset($tempArr);
            CacheService::set($this->addon_event, $event);
        }

        return $event;
    }

    /**
     * 获取命令信息
     * @param bool $is_refresh 是否刷新
     * @return array
     */
    final public function getCommand($is_refresh = false): array
    {
        $command = CacheService::get($this->addon_command, []);
        if($is_refresh) {
            $command = [];
        } else {
            if ($command) return $command;
        }

        $commandFile = $this->addon_path . 'command.php';
        if (is_file($commandFile)) {
            $tempArr = (array) include $commandFile;
            foreach ($tempArr as $key => $value) {
                $command[$key] = $value;
            }
            unset($tempArr);
            CacheService::set($this->addon_command, $command);
        }

        return $command;
    }

    /**
     * 清理缓存
     * @return void
     */
    final public function clear()
    {
        CacheService::delete($this->addon_info);
        CacheService::delete($this->addon_config);
        CacheService::delete($this->addon_event);
        CacheService::delete($this->addon_command);
    }

    /**
     * 插件安装
     */
    abstract public function install();

    /**
     * 插件卸载
     */
    abstract public function uninstall();

    /**
     * 插件更新
     */
    abstract public function update();

}